/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ui.gui.actions;

import java.awt.event.ActionEvent;
import java.io.File;

import javax.swing.AbstractAction;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import ui.WindowManager;
import ui.gui.documents.AbstractDocument;
import ui.gui.documents.EditorFactory;

/**
 * 
 * @author stephane
 */
public class SaveAsFileAction extends AbstractAction {
	private static final long serialVersionUID = 1L;

	public SaveAsFileAction(JFrame parent) {
		super("Save file as ...", new javax.swing.ImageIcon(
				SaveAsFileAction.class.getResource("/resources/saveFile.png")));
		putValue(SHORT_DESCRIPTION, "Save file as ...");
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		AbstractDocument selectedDocument = WindowManager.getInstance()
				.getSelectedDocument();
		if (selectedDocument != null) {
			try {

				JFileChooser fileChooser = new JFileChooser();

				for (int i = 0; i < EditorFactory.getInstance()
						.getExtensionFilter().length; i++) {
					fileChooser
							.addChoosableFileFilter(new DefaultFileFilter(
									EditorFactory.getInstance()
											.getExtensionFilter()[i],
									EditorFactory.getInstance()
											.getDescriptionFilter()[i]));

				}
				
				fileChooser.showSaveDialog(null);
				File file = fileChooser.getSelectedFile();
				if (file != null) {
					selectedDocument.setFile(file);
					selectedDocument.OnSave();
					selectedDocument.setIsDirty(false);
				}
			} catch (Exception ex) {
				System.out.println(ex);
			}
		}
	}
}